﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using COC.Commons;
// ReSharper disable UseObjectOrCollectionInitializer

namespace COC.Tool
{
    public partial class NewPlanTaskView : Form
    {
        private readonly CodingPlan _CodingPlan = new CodingPlan();
        private readonly PlanTaskManagerConfig _PlanTaskManagerConfig = new PlanTaskManagerConfig();
        private readonly List<PlanTask> _PlanTasks;
        private string _PlanTaskId;

        public NewPlanTaskView(List<PlanTask> pts, string id)
        {
            InitializeComponent();

            //导入状态
            for (var i = 0; i < _PlanTaskManagerConfig.TaskState.Count; i++)
                stateText.Items.Insert(i, _PlanTaskManagerConfig.TaskState[i]);
            //导入类别
            for (var i = 0; i < _PlanTaskManagerConfig.TaskType.Count; i++)
                typeText.Items.Insert(i, _PlanTaskManagerConfig.TaskType[i]);

            _PlanTasks = pts;
            if (id.Equals("0"))
            {
                IDText.Text = (_PlanTasks.Count + 1).ToString();
                stateText.Text = _PlanTaskManagerConfig.TaskState[0];
                typeText.Text = _PlanTaskManagerConfig.TaskType[0];
            }
            else
            {
                IDText.Text = id;
                stateText.Text = _CodingPlan.GetTaskFromId(_PlanTasks, id).State;
                typeText.Text = _CodingPlan.GetTaskFromId(_PlanTasks, id).Type;
                ContentText.Text = _CodingPlan.GetTaskFromId(_PlanTasks, id).Content;
            }
            _PlanTaskId = id;
        }

        private void AddPlanTaskYes_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ContentText.Text))
            {
                MessageBox.Show(@"任务内容为空，保存失败。");
                return;
            }
            if (_PlanTaskId.Equals("0"))
                AddPlanTask();
            else
                EditPlanTask();

            if (!string.IsNullOrEmpty(CommentText.Text))
                AddComment();
            Close();
        }

        private void AddComment()
        {
            var tc = new TaskComment();
            var _PlanTask = _CodingPlan.GetTaskFromId(_PlanTasks, _PlanTaskId);
            tc.Id = (_PlanTask.Comments.Count + 1).ToString();
            tc.Time = DateTime.Now.ToString("yyyy/MM/dd");
            tc.Content = CommentText.Text;
            _PlanTask.Comments.Add(tc);
            _CodingPlan.SaveNewComment(_PlanTask.Id, tc);
        }

        private void AddPlanTaskNo_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        ///     新建任务
        /// </summary>
        private void AddPlanTask()
        {
            var pt = new PlanTask();
            pt.Id = (_PlanTasks.Count + 1).ToString();
            pt.Type = typeText.Text;
            pt.State = stateText.Text;
            pt.InsData = DateTime.Now.ToString("yyyy/MM/dd");
            pt.FulFillData =
                pt.State.Equals(_PlanTaskManagerConfig.TaskState[2]) ||
                pt.State.Equals(_PlanTaskManagerConfig.TaskState[3])
                    ? DateTime.Now.ToString("yyyy/MM/dd")
                    : "";
            pt.Content = ContentText.Text;
            _PlanTasks.Reverse();
            _PlanTasks.Add(pt);
            _PlanTasks.Reverse();
            _CodingPlan.SaveNewPlanTask(pt);
            _PlanTaskId = pt.Id;
        }

        /// <summary>
        ///     编辑任务
        /// </summary>
        private void EditPlanTask()
        {
            var pt = _CodingPlan.GetTaskFromId(_PlanTasks, _PlanTaskId);
            pt.Type = typeText.Text;
            pt.State = stateText.Text;
            pt.FulFillData =
                pt.State.Equals(_PlanTaskManagerConfig.TaskState[2]) ||
                pt.State.Equals(_PlanTaskManagerConfig.TaskState[3])
                    ? DateTime.Now.ToString("yyyy/MM/dd")
                    : "";
            pt.Content = ContentText.Text;
            _CodingPlan.SaveEditPlanTask(pt);
        }
    }
}